package com.bite.demo.controller;

import com.bite.demo.constand.Constands;
import com.bite.demo.enums.StatusEnum;
import com.bite.demo.model.*;
import com.bite.demo.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.bite.demo.constand.Constands.USER_INFO_SESSION;

@Slf4j
@RequestMapping("/book")
@RestController

public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping("/getListByPage")
    public Result<PageResult<BookInfo>>getListByPage(RequestPage requestPage, HttpSession session) {
        log.info("接收参数getListPage：{}",requestPage);
        //1.参数校验

        PageResult<BookInfo> result =  bookService.getListByPage(requestPage);
        //成功
        return Result.success(result);
    }

    @RequestMapping("/addBook")
    public Boolean addBook(BookInfo bookInfo) {
        log.info("BookController.bookinfo 接收参数：{}",bookInfo);
        //1.参数校验
        if(!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() == null
                || bookInfo.getPrice() == null
                || !StringUtils.hasLength(bookInfo.getPublish())){
            log.warn("参数不正确");
            return  false;
        }
        //2.操作数据库
        Integer result = bookService.addBook(bookInfo);
        if(result == 0 ) {
            log.error("添加图书失败");
            return  false;
        }
        //3.返回结果
        log.info("添加图书成功");
        return true;
    }

    /**
     * 根据ID获取图书信息
     * @param bookId
     * @return
     */
    @RequestMapping("/getBookInfo")
    public BookInfo getBookInfo(Integer bookId) {
        log.info("getBookInfo,bookId:{}"+bookId);
        //需要看接口约定 当信息不存在时 需要null，还是返回对象
        BookInfo bookInfo =bookService.getBookInfo(bookId);
        if(bookId == null) {
            return new BookInfo();
        }
        return  bookInfo;
    }

    /**
     * 更新图书
     * @param bookInfo
     * @return
     */
    @RequestMapping("/updateBook")
    public Boolean updateBook(BookInfo bookInfo) {
        log.info("updateBook,bookInfo:{}"+bookInfo);
        if(bookInfo.getId() == null){
            log.warn("ID参数不为空");
            return  false;
        }
        if(bookInfo.getBookName()!=null && "".equals(bookInfo.getBookName().trim())) {
            log.warn("图书名称不能为空");
            return  false;
        }
        try {
            Integer result = bookService.updateBook(bookInfo);
            if(result == 1) {
                return  true;
            }
            return  false;
        } catch (Exception e) {
            log.error("updateBook error,e{}"+e);
            return false;
        }

    }
    @RequestMapping("/deleteBook")
    public Boolean deleteBook(Integer bookId) {
        log.warn("deleteBook,bookId:{}"+bookId);
        try {
            BookInfo bookInfo = new BookInfo();
            bookInfo.setId(bookId);
            bookInfo.setStatus(StatusEnum.DELETED.getCode());
            Integer result = bookService.updateBook(bookInfo);
            if(result == 1) {
                return  true;
            }
            return  false;
        } catch (Exception e) {
            log.error("updateBook error,e{}"+e);
            return false;
        }
    }

    @RequestMapping("/batchDelete")
    public boolean batchDelete(@RequestParam List<Integer> ids) {
        log.warn("batchDelete,ids:{}"+ids);
        if(ids.size()==0 || ids == null) {
            return  true;
        }
        try {
            Integer result=  bookService.batchDelete(ids);
            if(result > 0) {
                return true;
            }
            return false;
        }catch (Exception e) {
            log.error("batchDelete error:{}"+e);
            return false;
        }
    }

}
